package controllers.listeners.semrep;

import java.awt.Color;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;

import javax.swing.JLabel;
import javax.swing.JPanel;

import controllers.MainModuleController;

import models.FeaturesModel;
import objects.Constituent;
import objects.Feature;
import views.MainPanel;

/* Highlights the corresponding semantic/concept */
/* actionPerformed when you hover over a semantic/concept */
public class HighlightConstituentMouseListener implements MouseListener {
	
	private MainPanel view;
	
	private JPanel panel;
	private JLabel label;
	
	private Constituent currentConstituent;
	
	public HighlightConstituentMouseListener() {
		panel = null;
		label = null;
		
		currentConstituent = null;
	}
	
	@Override
	public void mouseClicked(MouseEvent evt) {
		panel.setBackground(Color.CYAN);
		label.setBackground(Color.CYAN);
		
		view.setCurrentPanel(panel);
		view.setCurrentLabel(label);

		int clickCount = evt.getClickCount();
		view.setCurrCon(currentConstituent);
		view.setClicked(true);
		
		if (clickCount < 2) {
			ArrayList<Feature> featureList = new ArrayList<>();
			
			view.loadFeature(currentConstituent);
			
			for (Constituent currentDbConstituent : FeaturesModel.getFeatures()) {
				if(currentConstituent.getLabel().equalsIgnoreCase(currentDbConstituent.getLabel())) {
					ArrayList<String> featureNames = new ArrayList<>();
					
					for (Feature currentFeature : currentConstituent.getFeatures().getFeatureList()) {
						featureNames.add(currentFeature.getName());
					}
					
					for (Feature currentDbFeature : currentDbConstituent.getFeatures().getFeatureList()) {
						if (!featureNames.contains(currentDbFeature.getName())) {
							if (!currentDbFeature.isEditable()) {
								featureList.add(currentDbFeature);
							}
						}
					}
				}
			}
			
			view.loadDefaultFeature(featureList);
			view.loadNoFeature();
		}
		else {
			try {
				MainModuleController.insertSemantic(currentConstituent, view);
			}catch (Exception ex) {
				ex.printStackTrace();
			}
		}
	}
	
	@Override
	public void mouseEntered(MouseEvent evt) {
		if (view.isClicked()) {
			view.getCurrentPanel().setOpaque(false);
			view.getCurrentPanel().setBackground(Color.GRAY);
			view.getCurrentLabel().setOpaque(false);
			view.getCurrentLabel().setBackground(Color.GRAY);
		}
		
		view.setClicked(false);
		panel.setOpaque(true);
		panel.setBackground(Color.YELLOW);
		label.setOpaque(true);
		label.setBackground(Color.YELLOW);
	}
	
	@Override
	public void mouseExited(MouseEvent evt) {
		if (!view.isClicked()) {
			panel.setOpaque(false);
			panel.setBackground(Color.GRAY);
			label.setOpaque(false);
			label.setBackground(Color.GRAY);
		}
	}
	
	@Override
	public void mousePressed(MouseEvent arg0) {}
	
	@Override
	public void mouseReleased(MouseEvent arg0) {}
	
	public void setPanel(JPanel panel) {
		this.panel = panel;
	}
	
	public void setLabel(JLabel label) {
		this.label = label;
	}
	
	public void setCurrentConstituent(Constituent constituent) {
		this.currentConstituent = constituent;
	}
	
	public void setView(MainPanel view) {
		this.view = view;
	}
}
